package com.tc.vms.consumer;

import com.tc.vms.iface.VmsQos;
import com.tc.vms.message.MessageConst;
import com.tc.vms.producer.MsgMode;

public class SubQoS extends VmsQos {

    private boolean autoCommit = false;
    private int prefetchSize = 1024 * 1024;
    private int prefetchCount = 64;
    private int commitInterval = 30000; //in ms
    private MsgMode msgMode = MsgMode.CONSUMER_CLUSTERING;

    public SubQoS() {
    }

    public SubQoS(final SubQoS subQoS) {
        this.autoCommit = subQoS.autoCommit;
        this.prefetchSize = subQoS.prefetchSize;
        this.prefetchCount = subQoS.prefetchCount;
        this.commitInterval = subQoS.commitInterval;
        this.msgMode = subQoS.msgMode;
    }

    public final long prefetchSize() {
        return this.prefetchSize;
    }

    public final int prefetchCount() {
        return this.prefetchCount;
    }

    public final boolean autoCommit() {
        return this.autoCommit;
    }

    public int getCommitInterval() {
        return commitInterval;
    }

    public SubQoS setCommitInterval(int commitInterval) {
        if (commitInterval > 0) {
            this.commitInterval = commitInterval;
        }
        return this;
    }

    public SubQoS prefetchSize(int prefetchSize) {
        if (prefetchSize > MessageConst.MESSAGE_MAX_SIZE) {
            this.prefetchSize = prefetchSize;
        }
        return this;
    }

    /**
     * Allow you to limit the number of unacknowledged messages on a channel (or
     * connection) when consuming (aka "prefetch count")
     *
     * @param prefetchCount The number of unacknowledged message in consumer
     * @return
     */
    public SubQoS prefetchCount(int prefetchCount) {
        this.prefetchCount = prefetchCount;
        return this;
    }

    public SubQoS autoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
        return this;
    }

    public MsgMode msgMode() {
        return msgMode;
    }

    public SubQoS msgMode(MsgMode msgMode) {
        this.msgMode = msgMode;
        return this;
    }

    public SubQoS cloneSubQoS() {
        return new SubQoS(this);
    }

}
